<template>
  <div>
    <el-button class="add" type="primary"  icon="el-icon-plus" @click="add">新增</el-button>
      <el-table :data="list" border style="width: 100%">
        <el-table-column prop="id" label="分类ID">
        </el-table-column>
        <el-table-column prop="classifyName" label="分类名称">
        </el-table-column>
        <el-table-column prop="order" label="排序"> </el-table-column>
        <el-table-column label="操作" width="180">
           <template slot-scope="scope">
            <el-button
                @click="edit(scope.row.id,scope)"
                type="text"
                size="small"
                class="eidt">
                修改
            </el-button>
            <!-- <el-button
                class="del"
                @click="del(scope.row.id,scope)"
                type="text"
                size="small">
                删除
            </el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          background
          @current-change="handleCurrentChange"
          layout="prev, pager, next"
          :current-page.sync="currentPage"
          :total="total">
      </el-pagination>    

     <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="分类名称" prop="classifyName" :label-width="formLabelWidth">
           <el-input v-model="form.classifyName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序值" prop="order" :label-width="formLabelWidth">
          <el-input v-model="form.order" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" v-if="isAdd" @click="addData">确 定</el-button>
        <el-button type="primary" v-if="!isAdd" @click="update">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      list: [],
      dialogFormVisible: false,
      title:'',
      total: null,
      currentPage: 1,
      form: {
         classifyName:'',
         order:''
      },
      rules: {
          classifyName: [
            { required: true, message: '请输入分类名称', trigger: 'blur' }
          ],
          order:[
            { required: true, message: '请输入排序值', trigger: 'blur' }
          ]
      },
      formLabelWidth: '120px',
      isAdd: true,
      id: ''
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.getList(val)
    },
    getList(page){
      // let token = localStorage.getItem('sessionId')
      let pra = {
        currentPage:page,
        pageSize: 10,
        query:{
          type: null
        }
      }
      this.$axios.post(`${this.$apiUrl}/gamemall/Classify/get`, pra)
        .then(res => {
          this.list = res.data.list
          this.total = res.data.total
        })
        .catch(error => {
          console.log(error);
        });
    },
    add(){
      this.title = '新增'
      this.isAdd = true
      this.form = {
         classifyName:'',
         order:'',
         type: 0
      }
      this.dialogFormVisible = true
    },
    addData(){
      this.$refs['form'].validate((valid) => {
        if (valid) {
          let pra = this.form
          this.$axios
          .post(`${this.$apiUrl}/gamemall/Classify/add`, pra)
          .then(res => {
            this.currentPage = 1
            this.getList(this.currentPage)
            this.dialogFormVisible = false
          })
          .catch(error => {
            console.log(error);
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    
    edit(id,scope){
      this.title = '修改'
      this.isAdd = false
      this.id = scope.row.id
      this.form = {
        classifyName: scope.row.classifyName,
        order: scope.row.order,
        type : 0
      }
      this.dialogFormVisible = true
    },

    update(){
      this.$refs['form'].validate((valid) => {
        if (valid) {
            let pra = this.form
            pra.id = this.id
            this.$axios
            .post(`${this.$apiUrl}/gamemall/Classify/update`, pra)
            .then(res => {
                this.dialogFormVisible = false
                this.getList(this.currentPage)
            })
            .catch(error => {
                console.log(error);
            }); 
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    del(id,scope){
      console.log(id)
    } 
  },
  created(){
    this.getList(this.currentPage)
  }
};
</script>
<style>

</style>
